#include "GetVersion.h"
#include "Protocol/ProtocolHelper.h"
#include "Log/ILogManager.h"


using namespace Command;

GetVersion::GetVersion()
	: TransactionCommand(VERSION_REQUEST, VERSION_RESPONSE)
{
}

void GetVersion::execute()
{
	QByteArray data;
	QDataStream writeStream(&data, QIODevice::WriteOnly);

	writeStream << (quint8) PROTOCOL_VERSION;

	sendData(data);
}

void GetVersion::processResponse(QByteArray& data)
{
	QDataStream readStream(&data, QIODevice::ReadOnly);

	quint8 messageType;
	quint16 protocolVersion;
	readStream >> messageType >> protocolVersion;

	if(protocolVersion != PROTOCOL_VERSION)
	{
		QString message = QString("Versions do not match. Host version: %1, device version: %2")
				.arg(PROTOCOL_VERSION)
				.arg(protocolVersion);

		core()->addLogMsg(message, Log::ILogManager::ERROR);
	}
	else
	{
		QString message = QString("Protocol version: %1")
				.arg(PROTOCOL_VERSION);

		core()->addLogMsg(message);
	}

	setResponseRecieved();
}
